<?php

#=============================
# Define database names
#=============================
define('DB_MEMBERS', 'zam_members');
define('DB_GROUPS', 'zam_groups');
define('DB_EXTRA', 'zam_mem_extra');
define('DB_IDENTS', 'zam_idents');
define('DB_SESSIONS', 'zam_sessions');

class sql {

	var $zam;
  
  	#=============================
	# Connect to database
	#=============================
  	function connect($host, $username, $password, $db)
  	{
    	$this->ident = @mysql_connect($host, $username, $password);
		
		if ( ! @mysql_select_db($db, $this->ident) )
		{
	 		$this->zam->kill('could not connect to MySQL database.');
		}
  	}
  
  	#=============================
	# Close connection
	#=============================
  	function close()
  	{
    	mysql_close();
  	}
  
  	#=============================
	# Kill and print out error
	#=============================
  	function error($string)
  	{
    	$this->zam->kill( 'mysql_error: ' . mysql_error() . '<br /> query: ' . $string );
  	}
  
  	#=============================
	# Execute query
	#=============================
  	function query($string)
  	{
    	$this->query = mysql_query($string);
			
		if ( !$this->query )
		{
	  		$this->error($string);
		}
	
		return $this->query;
  	}
	
	#=============================
	# Construct query
	#=============================
	function construct($data)
	{
		$query = '';
		
		# Actions - SELECT, INSERT, UPDATE, etc.
		if ( !array_key_exists('action', $data) )
		{
			$this->zam->kill('missing MySQL action in query.');
		}
		else
		{
			if ( !array_key_exists('db', $data) )
			{
				$this->zam->kill('missing database in query.');
			}
			
			if ( !array_key_exists('fields', $data) )
			{
				$data['fields'] = '*';
			}
			
			$actions = array(
				'insert'	=>	"INSERT INTO {$data['db']} ",
				'update'	=>	"UPDATE {$data['db']} SET ",
				'select'	=>	"SELECT {$data['fields']} FROM {$data['db']} ",
				'delete'	=>	"DELETE FROM {$data['db']} "
			);
			
			$query .= $actions[ $data['action'] ];
		}
		
		# INSERT key, values
		if ( $data['action'] == 'insert' )
		{
			while ( list($key, $value) = each($data['insert']) )
			{
				$keys .= "$key, ";
				$values .= "'$value', ";
			}
			
			$keys = preg_replace('/, $/', '', $keys);
			$values = preg_replace('/, $/', '', $values);
			
			$query .= "($keys) VALUES($values) ";
		}
		
		# UPDATE key = values
		if ( $data['action'] == 'update' )
		{
			while ( list($key, $value) = each($data['set']) )
			{
				$values .= "$key = '$value', ";
			}
			
			$values = preg_replace('/, $/', '', $values);
			$query .= "$values ";
		}
		
		# JOIN
		if ( array_key_exists('join', $data) )
		{
			$query .= "LEFT JOIN {$data['join']} ";
		}
		
		# WHERE clause
		if ( array_key_exists('where', $data) )
		{
			$query .= "WHERE {$data['where']} ";
		}
		
		# ORDER BY
		if ( array_key_exists('order', $data) )
		{
			$query .= "ORDER BY {$data['order']} ";
		}
		
		# GROUP BY
		if ( array_key_exists('group', $data) )
		{
			$query .= "GROUP BY {$data['group']} ";
		}
		
		# LIMIT x [, y]
		if ( array_key_exists('limit', $data) )
		{
			$query .= "LIMIT {$data['limit']}";
		}
		
		$this->query(trim($query));
	}
  
  	#=============================
	# Fetch data
	#=============================
  	function fetch($query='')
  	{
		if ( !$query )
  		{
  			$query = $this->query;
  		}
  		
		$this->rows = mysql_fetch_assoc($query);
		return $this->rows;
  	}
  
  	#=============================
	# Returns num results
	#=============================
  	function num_rows($query='')
  	{
  		if ( !$query )
  		{
  			$query = $this->query;
  		}
  		
    	$this->num_rows = mysql_num_rows($query);
		return $this->num_rows;
  	}
  	
  	#=============================
	# Returns affected rows
	#=============================
	function affected_rows()
	{
		$this->affected_rows = mysql_affected_rows( $this->ident );
		return $this->affected_rows;
	}
  
}

?>